//
//  QuDanSquareAttentionController.m
//  QuDanOEM
//
//  Created by mac on 2021/10/23.
//

#import "QuDanSquareAttentionController.h"
#import "QuDanSquareViewCell.h"
#import "QuDanHomeDetailController.h"
#import "QuDanSquareNetWork.h"
#import "QuDanSquareModel.h"
@interface QuDanSquareAttentionController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *cityTableView;
@property (nonatomic,strong)NSMutableArray *cityListArr;
@property (nonatomic,assign)int currentPage;
@property (nonatomic,assign)int pages;
@property (nonatomic,assign)BOOL hasMore;
@property (nonatomic,strong)QuDanSquareModel *attentionModel;
@end
@implementation QuDanSquareAttentionController
- (UITableView *)cityTableView
{
    if (!_cityTableView) {
        _cityTableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _cityTableView.backgroundColor = [UIColor clearColor];
        _cityTableView.showsVerticalScrollIndicator = NO;
        _cityTableView.showsHorizontalScrollIndicator = NO;
        _cityTableView.delegate = self;
        _cityTableView.dataSource = self;
        _cityTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_cityTableView registerNib:[UINib nibWithNibName:@"QuDanSquareViewCell" bundle:nil] forCellReuseIdentifier:@"QuDanSquareViewCell"];
    }
    return _cityTableView;
}
- (NSMutableArray *)cityListArr
{
    if (!_cityListArr) {
        _cityListArr = [NSMutableArray array];
    }
    return _cityListArr;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.cityTableView];
    [self.cityTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.offset(0);
    }];
    [self setupRefresh];
    [self loadAttentionNewData];
}
- (void)setupRefresh {
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadAttentionNewData)];
    MJRefreshBackNormalFooter *footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadAttentionMoreData)];
    header.automaticallyChangeAlpha = YES;
    header.lastUpdatedTimeLabel.hidden = YES;
    self.cityTableView.mj_header = header;
    self.cityTableView.mj_footer = footer;
}
-(void)loadAttentionNewData
{
    self.currentPage  = 1;
    [self roundLoadDataWithCurrentPageStr:[NSString stringWithFormat:@"%d",self.currentPage]];
}
-(void)roundLoadDataWithCurrentPageStr:(NSString *)currentPageStr
{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"mId"] = PostManage.shareInstance.userModel.ids;
    param[@"status"] = @"1";
    param[@"pageNo"] = currentPageStr;
    param[@"pageSize"] = @"10";
    param[@"type"] = @"2";
    [[QuDanSquareNetWork shareInstance]requestSquareRecommendOrAttention:param viewController:self andBlock:^(id  _Nullable data, BOOL result) {
        NSLog(@"QuDanSquareRecommController.广场关注.data:%@",data);
        self.attentionModel = [QuDanSquareModel mj_objectWithKeyValues:data];
        self.pages = (int)self.attentionModel.pages;
        if (self.pages > self.currentPage) {
            self.hasMore = YES;
        }else{
            self.hasMore = NO;
        }
        if([currentPageStr isEqualToString:@"1"]) {
            [self.cityListArr removeAllObjects];
            [self.cityListArr addObjectsFromArray:self.attentionModel.records];
        }else{
            [self.cityListArr addObjectsFromArray:self.attentionModel.records];
        }
        [self reloadData];
}];
}

-(void)reloadData{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.cityTableView reloadData];
        [self endRefresh];
    });
}
-(void)loadAttentionMoreData
{
    if (self.hasMore == YES) {
        self.currentPage++;
        [self roundLoadDataWithCurrentPageStr:[NSString stringWithFormat:@"%d",self.currentPage]];
    }else{
        [self endRefresh];
        [SVProgressHUD showInfoWithStatus:@"暂无更多数据"];
        return;
    }
}
-(void)endRefresh
{
    [self.cityTableView.mj_header endRefreshing];
    if(self.hasMore == NO  ){
        [self.cityTableView.mj_footer endRefreshingWithNoMoreData];
    }else{
        [self.cityTableView.mj_footer endRefreshing];
    }
}

#pragma mark -- QuDanSquareRecommController

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.cityListArr.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    QuDanSquareRecordsItem *model = self.cityListArr[indexPath.row];
    if (model.item.count == 0) {
        return 115 + 50;
    }else{
        return 115 + (QD_IPHONE_WIDTH -20)/3+10+40;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    QuDanSquareViewCell *squareCell = [tableView dequeueReusableCellWithIdentifier:@"QuDanSquareViewCell" forIndexPath:indexPath];
    squareCell.selectionStyle = UITableViewCellSelectionStyleNone;
    QuDanSquareRecordsItem *model = self.cityListArr[indexPath.row];
    squareCell.itemModel = model;
    return squareCell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    QuDanHomeDetailController *homedetailvc = [[QuDanHomeDetailController alloc]init];
    homedetailvc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:homedetailvc animated:YES];
}
@end
